package com.situ.mall.function.optional;

import com.situ.mall.function.pojo.Banji;
import com.situ.mall.function.pojo.Student;
import org.junit.jupiter.api.Test;

import javax.swing.text.html.Option;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class Demo {
    @Test
    public void test1() {
        Student student = getStudent();
        if (student != null) {
            System.out.println(student.getName());
            String name = student.getName();
            if (name != null) {
                System.out.println(name.length());
            }
        }
    }

    @Test
    public void test2() {
        Student student = getStudent();
        Optional<Student> studentOptional = Optional.ofNullable(student);
        studentOptional.ifPresent(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student.getName());
            }
        });
        studentOptional.ifPresent(stu -> System.out.println(stu.getName()));

        Optional.ofNullable(student).ifPresent(stu -> System.out.println(stu.getName()));
    }

    @Test
    public void test55() {
        Optional<Student> studentOptional = getStudentOptional();
        studentOptional.ifPresent(stu -> System.out.println(stu.getName()));
    }

    @Test
    public void test4() {
        Optional<Student> studentOptional = getStudentOptional();
        //java.util.NoSuchElementException: No value present
        //Student student1 = studentOptional.get();

        Student student2 = studentOptional.orElseGet(() -> new Student(2, "lisi", 23));
        System.out.println(student2.getName());

        try {
            Student student3 = studentOptional.orElseThrow((Supplier<Throwable>) () -> new RuntimeException("student为空"));
            System.out.println(student3.getName());
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }

    }

    @Test
    public void test7() {
        Optional<Student> studentOptional = getStudentOptional();
        studentOptional.filter(student -> student.getAge() > 18)
                .ifPresent(student -> System.out.println(student.getName()));
    }

    @Test
    public void test255() {
        Optional<Student> studentOptional = getStudentOptional();
        if (studentOptional.isPresent()) {
            Student student = studentOptional.get();
            System.out.println(student.getName());
        }

    }



    private Optional<Student> getStudentOptional() {
        Student student = new Student(1, "zhangsan", 12);
        return Optional.ofNullable(student);
    }

    private Student getStudent() {
        Student student = new Student(1, "zhangsan", 23);
        return student;
    }

    public Optional<Banji> getBanjiOptional() {
        Banji banji = new Banji(1, "Java230701", null);

        List<Student> students = new ArrayList<>();
        students.add(new Student(1, "张三1", 23));
        students.add(new Student(2, "张三2", 33));
        students.add(new Student(3, "张三3", 16));

        banji.setStudents(students);
        return Optional.ofNullable(banji);
    }
}
